﻿using System;
using static System.Console;

namespace Wrox.ProCSharp.Arrays
{
    class Program
    {
        static void Main()
        {
            //引用类型数组
            SimpleArrays();
            //二维数组
            TwoDim();
            //三维数组
            ThreeDim();
            //锯齿数组
            Jagged();
            //使用Array类创建数组
            ArrayClass();
            //引用对象浅复制
            CopyArrays();
            ReadKey();
        }

        static void CopyArrays()
        {
            Person[] beatles = {
                     new Person { FirstName="John", LastName="Lennon" },
                     new Person { FirstName="Paul", LastName="McCartney" }
                   };
            //Clone和Copy都会实现浅拷贝，但Clone方法会创建一个新数组，而Copy必须传递阶数和足够元素的已有数组
            //Clone
            //Person[] beatlesClone = (Person[])beatles.Clone();
            //beatlesClone[0].FirstName = "WaHaHa";
            //Copy
            Person[] beatlesCopy = new Person[beatles.Length];//必须new一个数组来实现Copy方法
            Array.Copy(beatles, beatlesCopy, beatles.Length);
            beatlesCopy[0].FirstName = "WaHaHa";

            WriteLine(beatles[0]);

        }

        static void ArrayClass()
        {
            //array类创建类型为int,大小为5的数组
            Array intArray1 = Array.CreateInstance(typeof(int), 5);
            WriteLine("\nArray: ");
            for (int i = 0; i < 5; i++)
            {
                //给第i个元素赋值33
                intArray1.SetValue(33, i);
            }

            for (int i = 0; i < 5; i++)
            {
                WriteLine(intArray1.GetValue(i));
            }

            int[] lengths = { 2, 3 };
            int[] lowerBounds = { 1, 10 };
            //实例化Array类的CreateInstance静态方法重载版本，创建2x3的二维数组，
            //第一位索引基于1，第二位的索引基于10
            Array racers = Array.CreateInstance(typeof(Person), lengths, lowerBounds);
            racers.SetValue(new Person { FirstName = "Alain", LastName = "Prost" }, 1, 10);
            racers.SetValue(new Person{ FirstName = "Emerson", LastName = "Fittipaldi" }, 1, 11);
            racers.SetValue(new Person { FirstName = "Ayrton", LastName = "Senna" }, 1, 12);
            racers.SetValue(new Person { FirstName = "Michael", LastName = "Schumacher" }, 2, 10);
            racers.SetValue(new Person { FirstName = "Fernando", LastName = "Alonso" }, 2, 11);
            racers.SetValue(new Person { FirstName = "Jenson", LastName = "Button" }, 2, 12);

            Person[,] racers2 = (Person[,])racers;
            Person first = racers2[1, 10];
            Person last = racers2[2, 12];
        }
        //锯齿数组
        static void Jagged()
        {
            int[][] jagged = new int[3][];
            jagged[0] = new int[2] { 1, 2 };
            jagged[1] = new int[6] { 3, 4, 5, 6, 7, 8 };
            jagged[2] = new int[3] { 9, 10, 11 };

            WriteLine("Jagged array: ");
            for (int row = 0; row < jagged.Length; row++)
            {
                for (int element = 0;
                   element < jagged[row].Length; element++)
                {
                    WriteLine(
                       $"row: {row}, element: {element}, value: {jagged[row][element]}");
                }
            }
        }

        static void ThreeDim()
        {
            int[,,] threedim = {
                    { { 1, 2 }, { 3, 4 } },
                    { { 5, 6 }, { 7, 8 } },
                    { { 9, 10 }, { 11, 12 } }
                   };


            WriteLine("ThreeDim: " + threedim[0, 1, 1]);
        }

        static void TwoDim()
        {
            int[,] twodim = new int[3, 3];
            twodim[0, 0] = 1;
            twodim[0, 1] = 2;
            twodim[0, 2] = 3;
            twodim[1, 0] = 4;
            twodim[1, 1] = 5;
            twodim[1, 2] = 6;
            twodim[2, 0] = 7;
            twodim[2, 1] = 8;
            twodim[2, 2] = 9;
        }

        //使用引用类型的数组，Person类对象
        static void SimpleArrays()
        {
            Person[] myPersons = new Person[2];

            myPersons[0] = new Person { FirstName = "Ayrton", LastName = "Senna" };
            myPersons[1] = new Person { FirstName = "Michael", LastName = "Schumacher" };

            Person[] myPersons2 =
            {
                new Person { FirstName="Ayrton", LastName="Senna"},
                new Person { FirstName="Michael", LastName="Schumacher"}
            };
        }
    }
}
